package thread.pool;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Server {
	static Executor pool = Executors.newFixedThreadPool(5);

	public static void main(String[] args) {

		try (ServerSocket server = new ServerSocket(9000)) {
			server.setSoTimeout(10000);

			while (true) {
				final Socket socket = server.accept();

				Runnable r = new Runnable() {
					@Override
					public void run() {
						doWork(socket);
					}
				};
				pool.execute(r);
			}
		} catch (IOException ex) {

		}
	}

	static void doWork(Socket socket) {

		try (DataOutputStream out = new DataOutputStream(
				socket.getOutputStream())) {

			out.writeUTF("Thank you for connecting to "
					+ socket.getLocalSocketAddress() + "\nGoodbye!");
		} catch (IOException ex) {
			System.out.println(ex.toString());
		}
	}
}
